# -*- coding: utf-8 -*-
from django.http import HttpResponse
from django.template import Context
from django.shortcuts import render_to_response
from tools.scrape import Scraper
from tools.utils import Myerror

def extract(request):
    c={}
    if request.GET.has_key('url'):
        url = request.GET['url'].strip()    
        try:
            sr = Scraper( url )
            (content,imgs,score,detail) = sr.get_content()
            tip = '[confidence:%f]' % score
            
            c = {'url':url,
                 'content':content,
                 'imgs':imgs,
                 'tip':tip,
                 'detail':detail.replace('\n','<br />').replace('\t','&nbsp;&nbsp;&nbsp;&nbsp;'),
                 }
        except Myerror,e:
            c['content'] = e

    return render_to_response('main.html',Context(c))
